﻿module Sustainalytics.Accounts.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;

    export class AccountsService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $location: ng.ILocationService;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            $location: ng.ILocationService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects,
            $upload: any) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjectsAdmin = staticObjectsAdmin;
            this.$location = $location;
        }

        private accountsUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() + "/accountsserv/api/v1/accounts/";
        }

        public accountsGetCount(statusString: string, nameString: string, tier1_or_tier2: boolean = false): ng.IPromise<number> {

            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.accountsUrl(), { params: $.extend({ getcount: true }, {status: statusString, name: nameString, tier1_or_tier2: tier1_or_tier2 }) });
            promise
                .success(data => {
                    def.resolve(parseInt(<string>data));
                })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountsGetLastCrmSync(): ng.IPromise<string>{
            var ctx = this;

            var def = ctx.$q.defer();

            var promise = ctx.$http.get(ctx.accountsUrl(), { params: { returnLastCrmSyncDate: true } });
            promise
                .success(data => {
                    def.resolve(data);
                })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountsGetList(gridOpt: CommonAdmin.HttpGetOptionsDto, status: string, name: string, tier1_or_tier2: boolean = false): ng.IPromise<AccountsDto[]> {

            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.accountsUrl(), { params: $.extend({ status: status, name: name, tier1_or_tier2: tier1_or_tier2}, gridOpt) });
            promise
                .success(data => { def.resolve(data); })
                .error(err => { ctx.showError(err); def.reject(); });
            return def.promise;
        }

        public accountGet(id: string): ng.IPromise<AccountsDto> {
            var ctx = this;

            var def = this.$q.defer();

            var promise = this.$http.get(ctx.accountsUrl() + id, {});
            promise
                .success(data=> { def.resolve(data); })
                .error(err=> { ctx.showError(err); def.reject(); });

            return def.promise;
        }    

        // show error message for a given error code
        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjectsAdmin.getMessageByInternalCode(err['code']));
        }

    }

    var mod = angular.module(accountAdminModuleName);

    mod.service('AccountsService',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjectsAdmin', '$upload', AccountsService]);
}

        



 